import { parse } from 'url';
import { TableListItem, TableListParams } from './data.d';

const name = [
  '宏源大厦物业',
  '中国人民大学物业',
  '北京林业大学物业',
  '河北联合大学轻工业学院',
];

function getLogisticsCenterList(
  req: { url: any },
  res: {
    json: (
      arg0: { body: { list: TableListItem[]; count: number; } },
    ) => void;
  },
  u: any,
) {
  let new_url = u;
  if (!new_url || Object.prototype.toString.call(new_url) !== '[object String]') {
    new_url = req.url;
  }

  const count = 50;
  const params = (parse(new_url, true).query as unknown) as TableListParams;

  const { pageIndex, pageSize } = params;
  const starNum = (pageIndex - 1) * pageSize;
  const endNum = pageIndex * pageSize;

  const dataSource: TableListItem[] = [];

  [...new Array(endNum - starNum)].map((item, index) => {
    dataSource.push({
      id: starNum + index + 1,
      name: name[Math.floor(Math.random() * 4)],
      level: 1,
      pinyin: 'mingzi',
      gardenId: 12,
      gardenName: '花园小区',
      code: '1001',
      orgName: '社区事业部',
      share: '是',
      tariffpkgId: 12,
      tariffpkgName: '套餐一',
      address: '北京市朝阳区酒仙桥宏源大厦',
      img: 'aasda',
      serviceTime: '2019-01-21 12:00:00',
      servicePhone: '010-110022345',
      reserveTime: '2019-01-21 12:00:00',
      extraSms: '这是短信内容',
      pickPhone: 13333333333,
      staffNum: 12,
      openDate: '2019-01-21 12:00:00',
      longitude: '21.24566',
      latitude: '24.6557',
      geocode: '1224',
      status: 1,
      openCodeType: 1,
      createUser: '张三丰',
      createTime: '2019-01-21 12:00:00',
      updateUser: '张三丰',
      updateTime: '2019-01-21 12:00:00',
    });
    return dataSource;
  });

  const result = {
    code: 0,
    body: {
      list: dataSource,
      count,
    },
  };

  return res.json(result);
}

export default {
  'GET /logis/list': getLogisticsCenterList,
};
